package cn.bone.alibabacloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class FlowLimitController {

    @RequestMapping("/testA")
    public String testA(){
        return "--- test A";
    }

    @RequestMapping("/testB")
    public String testB(){

        log.info("test b");
        return "--- test B";
    }

    @RequestMapping("/testD")
    public String testD(){

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        log.info("-- test D 测试RT 平均响应时间服务降级");

        return "--- test D";
    }

    @RequestMapping("/testE")
    public String testE(){

        log.info("--- test E 测试异常比例服务降级");
        int i = 10/0;

        return "---test E";
    }

    @RequestMapping("/testHotKey")
    //使用了热点规则的限流，需要自己配置降级处理的方法，不然就会抛出异常
    @SentinelResource(value = "testHotKey", blockHandler = "handle_testHotKey")
    public String testHotKey(String p1, String p2){

        log.info("-- testHotKey 测试热点规则");
        return "-- testHotKey";
    }

    public String handle_testHotKey(String p1, String p2,
                                    BlockException exception){

        return "-- handle_testHotKey";
    }

}
